﻿using Yxinmt.Interface;
using Yxinmt.Utility;
using System;
using System.Collections.Generic;
using System.Windows.Controls;

namespace Yxinmt.Controls
{
    public class DockPanel : BaseControl, IControl
    {
        private System.Windows.Controls.DockPanel _DockPanel { get; set; }

        public DockPanel(Dictionary<string, object> property, IComponent component, bool isFactory = false)
        {
            Property = property;
            _Component = component;
            _IsFactory = isFactory;
            if (_IsFactory)
            {
                _FactoryPropertyList = new List<FactoryProperty>();
                _ControlType = typeof(System.Windows.Controls.DockPanel);
            }

            _DockPanel = new System.Windows.Controls.DockPanel();
            this.Element = _DockPanel;

            InitSet();
            this.SetProperty();
            this.SetControlProperty(false, false);
        }

        public override void Add(System.Windows.FrameworkElement control)
        {
            _DockPanel.Children.Add(control);
        }

        public override void Remove(System.Windows.FrameworkElement control)
        {
            _DockPanel.Children.Remove(control);
        }

        public override void Clear()
        {
            if (_DockPanel.Children.Count > 0) _DockPanel.Children.Clear();
        }

        private void SetProperty()
        {
        }

        protected override void SetProperty(string name, object value)
        {
            System.Windows.Controls.DockPanel c = _DockPanel;
            switch (name)
            {
                case "name": c.Name = string.Concat("dpnl_", (string)value); SetFactoryProperty("Name", c.Name); break;
                case "Height": c.Height = Convert.ToDouble(value); SetFactoryProperty("Height", c.Height); break;
                case "Width": c.Width = Convert.ToDouble(value); SetFactoryProperty("Width", c.Width); break;
                case "Dock": System.Windows.Controls.DockPanel.SetDock(c, this.GetDock((string)value)); break;
                case "BackColor": c.Background = GetColor((string)value); SetFactoryProperty("Background", c.Background); break;
                case "ControlAlign": SetControlAlign((string)value); break;
                case "Visible": SetVisible(Convert.ToBoolean(value)); break;
                case "GridRowColumn": SetGridRowColumn((string)value); break;
                case "BackImageUrl": SetBackgroundImage((string)value); break;
                case "ZIndex": SetZIndex(Convert.ToInt32(value)); break;
                case "CanvasLocation": SetCanvasLocation((string)value); break;
                case "CanvasZIndex": SetCanvasZIndex(Convert.ToInt32(value)); break;
                case "Margin": c.Margin = this.GetThickness((string)value); SetFactoryProperty("Margin", c.Margin); break;
            }
        }

        protected override void SetFactoryProperty(string name, object value)
        {
            if (!_IsFactory) return;

            switch (name)
            {
                case "Margin": this.AddFactoryProperty(System.Windows.Controls.DockPanel.MarginProperty, value); break;
                case "Name": this.AddFactoryProperty(System.Windows.Controls.DockPanel.NameProperty, value); break;
                case "Height": this.AddFactoryProperty(System.Windows.Controls.DockPanel.HeightProperty, value); break;
                case "Width": this.AddFactoryProperty(System.Windows.Controls.DockPanel.WidthProperty, value); break;
                case "VerticalAlignment": this.AddFactoryProperty(System.Windows.Controls.DockPanel.VerticalAlignmentProperty, value); break;
                case "HorizontalAlignment": this.AddFactoryProperty(System.Windows.Controls.DockPanel.HorizontalAlignmentProperty, value); break;
                case "Visibility": this.AddFactoryProperty(System.Windows.Controls.DockPanel.VisibilityProperty, value); break;
                case "Background": this.AddFactoryProperty(System.Windows.Controls.DockPanel.BackgroundProperty, value); break;
            }
        }

        public override void EventLoad()
        {
            base.EventLoad();

            bool isClick = Property.GetValue<bool>("IsClick");
            bool isDoubleClick = Property.GetValue<bool>("IsDoubleClick");
            bool isMouseOver = Property.GetValue<bool>("IsMouseOver");

            if (isClick || isDoubleClick)
            {
                ClickAction = (sender, e, ctl) =>
                {
                    if (_DataGridViewControls != null) _DataGridViewControls.DataGridView.EventInvoke(sender, e, this, "RowClick");
                };

                DoubleClickAction = (sender, e, ctl) =>
                {
                    if (_DataGridViewControls != null) _DataGridViewControls.DataGridView.EventInvoke(sender, e, this, "RowDoubleClick");
                };

                bool isMouseDown = Property.GetValue<bool>("IsMouseDown");
                bool isMouseUp = Property.GetValue<bool>("IsMouseUp");

                if (isMouseDown)
                {
                    MouseDownAction = (sender, e, ctl) =>
                    {
                        if (_DataGridViewControls != null) _DataGridViewControls.DataGridView.EventInvoke(sender, e, this, "RowMouseDown");
                    };
                }

                if (isMouseUp)
                {
                    MouseUpAction = (sender, e, ctl) =>
                    {
                        if (_DataGridViewControls != null) _DataGridViewControls.DataGridView.EventInvoke(sender, e, this, "RowMouseUp");
                    };
                }

                BindClick(isClick, isDoubleClick);
            }

            if (isMouseOver)
            {
                MouseEnter = (sender, e) =>
                {
                    if (_DataGridViewControls != null) _DataGridViewControls.DataGridView.EventInvoke(sender, e, this, "RowMouseEnter");
                };

                MouseLeave = (sender, e) =>
                {
                    if (_DataGridViewControls != null) _DataGridViewControls.DataGridView.EventInvoke(sender, e, this, "RowMouseLeave");
                };

                BindMouseOver();
            }
        }
    }
}
